{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/madaan/memprompt/blob/main/YoavsPythonPrompts.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "!rm -rf prompt-lib\n",
        "!git clone https://github.com/reasoning-machines/prompt-lib\n",
        "!pip install prompt-lib/"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "HQvxP7ZbhTTK",
        "outputId": "aeab582e-b6f0-42d6-d1a4-d42bc9d72694"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Cloning into 'prompt-lib'...\n",
            "remote: Enumerating objects: 481, done.\u001b[K\n",
            "remote: Counting objects: 100% (481/481), done.\u001b[K\n",
            "remote: Compressing objects: 100% (276/276), done.\u001b[K\n",
            "remote: Total 481 (delta 283), reused 388 (delta 196), pack-reused 0\u001b[K\n",
            "Receiving objects: 100% (481/481), 2.02 MiB | 29.20 MiB/s, done.\n",
            "Resolving deltas: 100% (283/283), done.\n",
            "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n",
            "Processing ./prompt-lib\n",
            "\u001b[33m  DEPRECATION: A future pip version will change local packages to be built in-place without first copying to a temporary directory. We recommend you use --use-feature=in-tree-build to test your packages with this new behavior before it becomes the default.\n",
            "   pip 21.3 will remove support for this functionality. You can find discussion regarding this at https://github.com/pypa/pip/issues/7555.\u001b[0m\n",
            "Collecting openai==0.23.0\n",
            "  Downloading openai-0.23.0.tar.gz (43 kB)\n",
            "\u001b[K     |████████████████████████████████| 43 kB 2.2 MB/s \n",
            "\u001b[?25h  Installing build dependencies ... \u001b[?25l\u001b[?25hdone\n",
            "  Getting requirements to build wheel ... \u001b[?25l\u001b[?25hdone\n",
            "    Preparing wheel metadata ... \u001b[?25l\u001b[?25hdone\n",
            "Requirement already satisfied: pandas in /usr/local/lib/python3.8/dist-packages (from prompt-lib==0.1) (1.3.5)\n",
            "Collecting tqdm==4.49.0\n",
            "  Downloading tqdm-4.49.0-py2.py3-none-any.whl (69 kB)\n",
            "\u001b[K     |████████████████████████████████| 69 kB 7.9 MB/s \n",
            "\u001b[?25hCollecting wandb==0.13.2\n",
            "  Downloading wandb-0.13.2-py2.py3-none-any.whl (1.8 MB)\n",
            "\u001b[K     |████████████████████████████████| 1.8 MB 87.0 MB/s \n",
            "\u001b[?25hRequirement already satisfied: typing-extensions in /usr/local/lib/python3.8/dist-packages (from openai==0.23.0->prompt-lib==0.1) (4.1.1)\n",
            "Requirement already satisfied: openpyxl>=3.0.7 in /usr/local/lib/python3.8/dist-packages (from openai==0.23.0->prompt-lib==0.1) (3.0.10)\n",
            "Requirement already satisfied: requests>=2.20 in /usr/local/lib/python3.8/dist-packages (from openai==0.23.0->prompt-lib==0.1) (2.23.0)\n",
            "Requirement already satisfied: numpy in /usr/local/lib/python3.8/dist-packages (from openai==0.23.0->prompt-lib==0.1) (1.21.6)\n",
            "Collecting pandas-stubs>=1.1.0.11\n",
            "  Downloading pandas_stubs-1.5.2.221124-py3-none-any.whl (146 kB)\n",
            "\u001b[K     |████████████████████████████████| 146 kB 65.6 MB/s \n",
            "\u001b[?25hCollecting pathtools\n",
            "  Downloading pathtools-0.1.2.tar.gz (11 kB)\n",
            "Requirement already satisfied: PyYAML in /usr/local/lib/python3.8/dist-packages (from wandb==0.13.2->prompt-lib==0.1) (6.0)\n",
            "Requirement already satisfied: setuptools in /usr/local/lib/python3.8/dist-packages (from wandb==0.13.2->prompt-lib==0.1) (57.4.0)\n",
            "Collecting shortuuid>=0.5.0\n",
            "  Downloading shortuuid-1.0.11-py3-none-any.whl (10 kB)\n",
            "Collecting GitPython>=1.0.0\n",
            "  Downloading GitPython-3.1.29-py3-none-any.whl (182 kB)\n",
            "\u001b[K     |████████████████████████████████| 182 kB 66.3 MB/s \n",
            "\u001b[?25hRequirement already satisfied: six>=1.13.0 in /usr/local/lib/python3.8/dist-packages (from wandb==0.13.2->prompt-lib==0.1) (1.15.0)\n",
            "Requirement already satisfied: promise<3,>=2.0 in /usr/local/lib/python3.8/dist-packages (from wandb==0.13.2->prompt-lib==0.1) (2.3)\n",
            "Requirement already satisfied: Click!=8.0.0,>=7.0 in /usr/local/lib/python3.8/dist-packages (from wandb==0.13.2->prompt-lib==0.1) (7.1.2)\n",
            "Collecting setproctitle\n",
            "  Downloading setproctitle-1.3.2-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (31 kB)\n",
            "Requirement already satisfied: protobuf<4.0dev,>=3.12.0 in /usr/local/lib/python3.8/dist-packages (from wandb==0.13.2->prompt-lib==0.1) (3.19.6)\n",
            "Collecting sentry-sdk>=1.0.0\n",
            "  Downloading sentry_sdk-1.11.1-py2.py3-none-any.whl (168 kB)\n",
            "\u001b[K     |████████████████████████████████| 168 kB 50.7 MB/s \n",
            "\u001b[?25hCollecting docker-pycreds>=0.4.0\n",
            "  Downloading docker_pycreds-0.4.0-py2.py3-none-any.whl (9.0 kB)\n",
            "Requirement already satisfied: psutil>=5.0.0 in /usr/local/lib/python3.8/dist-packages (from wandb==0.13.2->prompt-lib==0.1) (5.4.8)\n",
            "Collecting gitdb<5,>=4.0.1\n",
            "  Downloading gitdb-4.0.10-py3-none-any.whl (62 kB)\n",
            "\u001b[K     |████████████████████████████████| 62 kB 1.4 MB/s \n",
            "\u001b[?25hCollecting smmap<6,>=3.0.1\n",
            "  Downloading smmap-5.0.0-py3-none-any.whl (24 kB)\n",
            "Requirement already satisfied: et-xmlfile in /usr/local/lib/python3.8/dist-packages (from openpyxl>=3.0.7->openai==0.23.0->prompt-lib==0.1) (1.1.0)\n",
            "Requirement already satisfied: python-dateutil>=2.7.3 in /usr/local/lib/python3.8/dist-packages (from pandas->prompt-lib==0.1) (2.8.2)\n",
            "Requirement already satisfied: pytz>=2017.3 in /usr/local/lib/python3.8/dist-packages (from pandas->prompt-lib==0.1) (2022.6)\n",
            "Collecting types-pytz>=2022.1.1\n",
            "  Downloading types_pytz-2022.6.0.1-py3-none-any.whl (4.7 kB)\n",
            "Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.8/dist-packages (from requests>=2.20->openai==0.23.0->prompt-lib==0.1) (3.0.4)\n",
            "Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.8/dist-packages (from requests>=2.20->openai==0.23.0->prompt-lib==0.1) (1.24.3)\n",
            "Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.8/dist-packages (from requests>=2.20->openai==0.23.0->prompt-lib==0.1) (2.10)\n",
            "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.8/dist-packages (from requests>=2.20->openai==0.23.0->prompt-lib==0.1) (2022.9.24)\n",
            "Collecting sentry-sdk>=1.0.0\n",
            "  Downloading sentry_sdk-1.11.0-py2.py3-none-any.whl (168 kB)\n",
            "\u001b[K     |████████████████████████████████| 168 kB 67.6 MB/s \n",
            "\u001b[?25h  Downloading sentry_sdk-1.10.1-py2.py3-none-any.whl (166 kB)\n",
            "\u001b[K     |████████████████████████████████| 166 kB 69.8 MB/s \n",
            "\u001b[?25h  Downloading sentry_sdk-1.10.0-py2.py3-none-any.whl (166 kB)\n",
            "\u001b[K     |████████████████████████████████| 166 kB 93.3 MB/s \n",
            "\u001b[?25h  Downloading sentry_sdk-1.9.10-py2.py3-none-any.whl (162 kB)\n",
            "\u001b[K     |████████████████████████████████| 162 kB 45.8 MB/s \n",
            "\u001b[?25h  Downloading sentry_sdk-1.9.9-py2.py3-none-any.whl (162 kB)\n",
            "\u001b[K     |████████████████████████████████| 162 kB 72.1 MB/s \n",
            "\u001b[?25h  Downloading sentry_sdk-1.9.8-py2.py3-none-any.whl (158 kB)\n",
            "\u001b[K     |████████████████████████████████| 158 kB 70.8 MB/s \n",
            "\u001b[?25h  Downloading sentry_sdk-1.9.7-py2.py3-none-any.whl (157 kB)\n",
            "\u001b[K     |████████████████████████████████| 157 kB 54.9 MB/s \n",
            "\u001b[?25h  Downloading sentry_sdk-1.9.6-py2.py3-none-any.whl (157 kB)\n",
            "\u001b[K     |████████████████████████████████| 157 kB 66.8 MB/s \n",
            "\u001b[?25h  Downloading sentry_sdk-1.9.5-py2.py3-none-any.whl (157 kB)\n",
            "\u001b[K     |████████████████████████████████| 157 kB 87.0 MB/s \n",
            "\u001b[?25h  Downloading sentry_sdk-1.9.4-py2.py3-none-any.whl (157 kB)\n",
            "\u001b[K     |████████████████████████████████| 157 kB 70.5 MB/s \n",
            "\u001b[?25h  Downloading sentry_sdk-1.9.3-py2.py3-none-any.whl (157 kB)\n",
            "\u001b[K     |████████████████████████████████| 157 kB 72.2 MB/s \n",
            "\u001b[?25h  Downloading sentry_sdk-1.9.2-py2.py3-none-any.whl (157 kB)\n",
            "\u001b[K     |████████████████████████████████| 157 kB 72.2 MB/s \n",
            "\u001b[?25h  Downloading sentry_sdk-1.9.1-py2.py3-none-any.whl (157 kB)\n",
            "\u001b[K     |████████████████████████████████| 157 kB 69.6 MB/s \n",
            "\u001b[?25h  Downloading sentry_sdk-1.9.0-py2.py3-none-any.whl (156 kB)\n",
            "\u001b[K     |████████████████████████████████| 156 kB 70.4 MB/s \n",
            "\u001b[?25hBuilding wheels for collected packages: prompt-lib, openai, pathtools\n",
            "  Building wheel for prompt-lib (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
            "  Created wheel for prompt-lib: filename=prompt_lib-0.1-py3-none-any.whl size=42659 sha256=f09c002bef1b5751a7ad8fdba234d51303b3cf7976ff18aa069b3369cba60cf8\n",
            "  Stored in directory: /root/.cache/pip/wheels/c7/4e/3b/343a7fd97925b48a3a6b06472e580485848588ceb175e298ca\n",
            "  Building wheel for openai (PEP 517) ... \u001b[?25l\u001b[?25hdone\n",
            "  Created wheel for openai: filename=openai-0.23.0-py3-none-any.whl size=54478 sha256=e8f154c58e7c706964c602cf48e6accff702329c0d4e7df8d688a27a9fc27b33\n",
            "  Stored in directory: /root/.cache/pip/wheels/80/3d/df/3191cdb7a0394b8fdee4f2f8212e08475bb618d7fd845f1951\n",
            "  Building wheel for pathtools (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
            "  Created wheel for pathtools: filename=pathtools-0.1.2-py3-none-any.whl size=8806 sha256=c2c2f2ef03894c736b0c3909b6d85359971e415865a612a19431f8ef6ab970d5\n",
            "  Stored in directory: /root/.cache/pip/wheels/4c/8e/7e/72fbc243e1aeecae64a96875432e70d4e92f3d2d18123be004\n",
            "Successfully built prompt-lib openai pathtools\n",
            "Installing collected packages: smmap, types-pytz, gitdb, tqdm, shortuuid, setproctitle, sentry-sdk, pathtools, pandas-stubs, GitPython, docker-pycreds, wandb, openai, prompt-lib\n",
            "  Attempting uninstall: tqdm\n",
            "    Found existing installation: tqdm 4.64.1\n",
            "    Uninstalling tqdm-4.64.1:\n",
            "      Successfully uninstalled tqdm-4.64.1\n",
            "Successfully installed GitPython-3.1.29 docker-pycreds-0.4.0 gitdb-4.0.10 openai-0.23.0 pandas-stubs-1.5.2.221124 pathtools-0.1.2 prompt-lib-0.1 sentry-sdk-1.9.0 setproctitle-1.3.2 shortuuid-1.0.11 smmap-5.0.0 tqdm-4.49.0 types-pytz-2022.6.0.1 wandb-0.13.2\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Import prompt-lib"
      ],
      "metadata": {
        "id": "RWNZOLGb2G8A"
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "BQdqJ5ZmNuLw"
      },
      "outputs": [],
      "source": [
        "import os"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "os.environ[\"OPENAI_API_KEY\"] = 'sk-' #@param {type:\"string\"}\n",
        "!export OPENAI_API_KEY"
      ],
      "metadata": {
        "id": "a39F_ly9hKzy"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "from prompt_lib.backends import openai_api"
      ],
      "metadata": {
        "id": "0G8Z3P7rNwuU"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "### A \"terminal\" that grows the prompt (https://arxiv.org/abs/2201.06009)"
      ],
      "metadata": {
        "id": "E68N6Gdf2JoA"
      }
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "oez91_qjxKX0"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "class Terminal:\n",
        "\n",
        "  def __init__(self, history):\n",
        "    self.history = history\n",
        "\n",
        "  def __call__(self, cmd: str) -> str:\n",
        "    self.history = f\"{self.history}{cmd}\"\n",
        "    print(self.history)\n",
        "    response = openai_api.OpenaiAPIWrapper.call(prompt=self.history, engine=\"code-davinci-002\", max_tokens=100, stop_token=\">>>\", temperature=0.0)\n",
        "    print(openai_api.OpenaiAPIWrapper.get_first_response(response))"
      ],
      "metadata": {
        "id": "vbfBfhT4waf7"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Seed prompt"
      ],
      "metadata": {
        "id": "JsA2N_Wq2PhG"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "terminal = Terminal(\"\"\"\n",
        "# emulates a Python terminal:\n",
        "\n",
        "# now run this\n",
        ">>> y=13\n",
        ">>> print(y)\n",
        "13\n",
        "\n",
        "# now run this\n",
        ">>> z = sum(list(range(5)))\n",
        ">>> print(z)\n",
        "10\n",
        "\n",
        "# now run this\n",
        ">>> lst = list(range(5))[::-1]\n",
        ">>> lst\n",
        "[4, 3, 2, 1, 0]\n",
        "\n",
        "\"\"\"\n",
        ")"
      ],
      "metadata": {
        "id": "EDowevP1xEaj"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Print command"
      ],
      "metadata": {
        "id": "dupYXO9_1yjj"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "print_cmd = \"\"\"\n",
        "# now run this\n",
        "\n",
        ">>> x=5\n",
        ">>> print(x)\n",
        "\"\"\"\n",
        "terminal(print_cmd)\n"
      ],
      "metadata": {
        "id": "h3lKQJ6TSvuc",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "1b83904e-4968-4f3d-ed30-41a617019fbb"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "\n",
            "# emulates a Python terminal:\n",
            "\n",
            "# now run this\n",
            ">>> y=13\n",
            ">>> print(y)\n",
            "13\n",
            "\n",
            "# now run this\n",
            ">>> z = sum(list(range(5)))\n",
            ">>> print(z)\n",
            "10\n",
            "\n",
            "# now run this\n",
            ">>> lst = list(range(5))[::-1]\n",
            ">>> lst\n",
            "[4, 3, 2, 1, 0]\n",
            "\n",
            "\n",
            "# now run this\n",
            "\n",
            ">>> x=5\n",
            ">>> print(x)\n",
            "\n",
            "5\n",
            "\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Loop command"
      ],
      "metadata": {
        "id": "7lpMBu101091"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "loop_cmd = \"\"\"\n",
        "# now run this\n",
        "\n",
        ">>> for i in range(9):\n",
        "...     x += 1\n",
        "...     print(x)\n",
        "\"\"\"\n",
        "terminal(loop_cmd)"
      ],
      "metadata": {
        "id": "YTindk1cSxTD",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "78d0f283-0217-4adf-dcd1-ed636a75ef28"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "\n",
            "# emulates a Python terminal:\n",
            "\n",
            "# now run this\n",
            ">>> y=13\n",
            ">>> print(y)\n",
            "13\n",
            "\n",
            "# now run this\n",
            ">>> z = sum(list(range(5)))\n",
            ">>> print(z)\n",
            "10\n",
            "\n",
            "# now run this\n",
            ">>> lst = list(range(5))[::-1]\n",
            ">>> lst\n",
            "[4, 3, 2, 1, 0]\n",
            "\n",
            "\n",
            "# now run this\n",
            "\n",
            ">>> x=5\n",
            ">>> print(x)\n",
            "\n",
            "# now run this\n",
            "\n",
            ">>> for i in range(9):\n",
            "...     x += 1\n",
            "...     print(x)\n",
            "\n",
            "...\n",
            "6\n",
            "7\n",
            "8\n",
            "9\n",
            "10\n",
            "11\n",
            "12\n",
            "13\n",
            "14\n",
            "\n",
            "# now run this\n",
            "\n",
            "\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Function command"
      ],
      "metadata": {
        "id": "QM6YuiEa121N"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "foo_cmd = \"\"\"\n",
        "# now run this\n",
        "\n",
        ">>> def foo(x, y):\n",
        "...     return x+y\n",
        "...\n",
        ">>> list(map(foo, zip([1,2,3],[4,5,6])))\n",
        "\"\"\"\n",
        "terminal(foo_cmd)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "jSJ9gcfhsKrV",
        "outputId": "edebb36d-a234-4dc3-8bb9-a9a8d5442082"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "\n",
            "# emulates a Python terminal:\n",
            "\n",
            "# now run this\n",
            ">>> y=13\n",
            ">>> print(y)\n",
            "13\n",
            "\n",
            "# now run this\n",
            ">>> z = sum(list(range(5)))\n",
            ">>> print(z)\n",
            "10\n",
            "\n",
            "# now run this\n",
            ">>> lst = list(range(5))[::-1]\n",
            ">>> lst\n",
            "[4, 3, 2, 1, 0]\n",
            "\n",
            "\n",
            "# now run this\n",
            "\n",
            ">>> x=5\n",
            ">>> print(x)\n",
            "\n",
            "# now run this\n",
            "\n",
            ">>> for i in range(9):\n",
            "...     x += 1\n",
            "...     print(x)\n",
            "\n",
            "# now run this\n",
            "\n",
            ">>> def foo(x, y):\n",
            "...     return x+y\n",
            "...\n",
            ">>> list(map(foo, zip([1,2,3],[4,5,6])))\n",
            "\n",
            "[5, 7, 9]\n",
            "\n",
            "# now run this\n",
            "\n",
            "\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "3JXZt0g1sWDO"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}